package com.mike.uaa.core;

import cn.hutool.core.util.StrUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author z
 */
public class SimpleGrantedAuthority implements GrantedAuthority {

    private static final long serialVersionUID = 1944660992125502463L;

    private final String role;


    public SimpleGrantedAuthority(String role) {
        this.role = role;
    }


    public static SimpleGrantedAuthority of(String role) {
        return new SimpleGrantedAuthority(role);
    }


    public static List<? extends GrantedAuthority> commaDelimitedListToList(String authorityString) {
        if (StrUtil.isBlank(authorityString)) {
            return new ArrayList<>();
        }
        return of(authorityString.split(StrUtil.COMMA));
    }


    public static List<? extends GrantedAuthority> of(String... authorities) {
        return Arrays.stream(authorities).map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    public static List<? extends GrantedAuthority> of(Collection<String> authorities) {
        return authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }


    @Override
    public String getAuthority() {
        return this.role;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SimpleGrantedAuthority that = (SimpleGrantedAuthority) o;
        return Objects.equals(role, that.role);
    }

    @Override
    public int hashCode() {
        return Objects.hash(role);
    }
}
